<?php

class menu
{
	public static function getMainMenu($if_submenu = true, $parent = 1)
	{
		$r =& Registry::get("MDB")->query("SELECT `ID`,`Name`,`Sys_name`,`Redirect` FROM `".PRFX."structure` WHERE `Show`='1' AND `Is_Menu`='1' AND `Parent`='".$parent."' ORDER BY `Por`;");
		if (PEAR::isError($r)) {
			die($r->getMessage().' ::: '.$r->getUserInfo());
		}		
		if($r->numRows())
		{						
			$ActiveParam = Registry::get("common_vars");
			while ($p = $r->fetchRow(MDB2_FETCHMODE_ASSOC))
			{				
				$MenuMainArray[$p['id']] = array(
					'name'      => $p['name'],
					'sys_name'  => $p['sys_name'],
					'redirect'  => $p['redirect'],
					'is_active' => (($ActiveParam["structure_id"] == $p['id']) ? 1 : 0)
				);			
				
				/* Субменю */
				if($if_submenu) {
					$rSubmenu =& Registry::get("MDB")->query("SELECT `ID`,`Name`,`Sys_name`,`Redirect` FROM `".PRFX."structure` WHERE `Show`='1' AND `Is_Menu`='1' AND `Parent`='".$p['id']."' ORDER BY `Por`;");
					if (PEAR::isError($rSubmenu)) {
						die($rSubmenu->getMessage().' ::: '.$rSubmenu->getUserInfo());
					}		
					if($rSubmenu->numRows())
					{			
						while ($pSubmenu = $rSubmenu->fetchRow(MDB2_FETCHMODE_ASSOC))
						{
							$MenuMainArray[$p['id']]["submenu"][$pSubmenu['id']] = array(
								'name'     => $pSubmenu['name'],
								'sys_name' => $pSubmenu['sys_name'],
								'redirect' => $pSubmenu['redirect']
							);
							
							if($ActiveParam["structure_id"] == $pSubmenu['id']) {
								$MenuMainArray[$p['id']]['is_active'] = 1;
							}
						}
					}
				}
			}
			// print_r($MenuMainArray);
			return $MenuMainArray;
		}
	}
	
	/* Субменю */
	public static function getSubMenu($id, $part_first)
	{
		if($id == "1") {
			return false;
		}
		$r =& Registry::get("MDB")->query("SELECT `ID`,`Parent`,`Name`,`Sys_name`,`Redirect` FROM `".PRFX."structure` WHERE `Show`='1' AND `Is_Menu`='1' AND `Parent`='".$id."' ORDER BY `Por`;");
		if (PEAR::isError($r)) {
			die($r->getMessage().' ::: '.$r->getUserInfo());
		}		
		if($r->numRows())
		{						
			while ($p = $r->fetchRow(MDB2_FETCHMODE_ASSOC)) {
				$p["is_active"] = (($part_first == $p["sys_name"]) ? 1 : '');
				$array[] = $p;				
			}
			return $array;
		}
	}
	
	/* Субменю текстовая страница */
	public static function getSubMenuPages($id, $part_first)
	{
		$r =& Registry::get("MDB")->query("SELECT `ID`,`Parent`,`Name`,`Sys_name` FROM `".PRFX."pages` WHERE `Show`='1' AND `Parent`='".$id."' ORDER BY `Por`;");
		if (PEAR::isError($r)) {
			die($r->getMessage().' ::: '.$r->getUserInfo());
		}		
		if($r->numRows())
		{						
			while ($p = $r->fetchRow(MDB2_FETCHMODE_ASSOC)) {
				$p["is_active"] = (($part_first == $p["sys_name"]) ? 1 : '');
				$array[] = $p;				
			}
			return $array;
		}
	}
	
	
	/* Новости, Акции */
	public static function getSubMenuNews($id, $limit = 5)
	{
		$r =& Registry::get("MDB")->query("SELECT `Name`,`Date`,`Stext`,`Sys_name`,`ID` FROM `".PRFX."news` WHERE `Show`='1' AND `Parent`='".$id."' ORDER BY `Por` LIMIT ".$limit.";");
		if (PEAR::isError($r)) {
			die($r->getMessage().' ::: '.$r->getUserInfo());
		}		
		if($r->numRows())
		{						
			while ($p = $r->fetchRow(MDB2_FETCHMODE_ASSOC)) {
				$p["image"] = functions::getImage("news", $p["id"], $p["name"], 1, "_106x75_news_trumbnail.jpg", "normal", "md5");
				$array[] = $p;				
			}
			return $array;
		}
	}
}

?>